package com.orchard.xpz.service.gardenWork.Impl;

import com.github.pagehelper.PageHelper;
import com.orchard.xpz.dao.GardenWorkDao;
import com.orchard.xpz.entity.GardenMedication.GardenMedication;
import com.orchard.xpz.entity.GardenWork.GardenWork;
import com.orchard.xpz.service.gardenWork.GardenWorkService;
import com.orchard.xpz.utils.PageResultData;
import com.orchard.xpz.utils.UserRuntime;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @TaskCode:
 * @description:
 * @author: minshuai.chen
 * @create: 2022/4/21 16:10
 **/
@Service
public class GardenWorkServiceImpl implements GardenWorkService {
    @Autowired
    private GardenWorkDao gardenWorkDao;

    @Override
    public PageResultData<GardenWork> getPageList(GardenWork gardenWork) {
        PageHelper.startPage(gardenWork.getPageNum(),gardenWork.getPageSize());
        List<GardenWork> list = gardenWorkDao.getList(gardenWork);
        PageResultData<GardenWork> pageResultData = new PageResultData<>(list, gardenWork.getPageNum(), gardenWork.getPageSize());
        return pageResultData;
    }

    @Override
    public boolean saveOrUpdate(GardenWork gardenWork) {
        gardenWork.setGardenId(gardenWork.getGardenPlot().getGardenId());
        if (ObjectUtils.isEmpty(gardenWork.getWorkId())){
            gardenWork.setCreateName(UserRuntime.getUser().getUserName());
            gardenWork.setCreateTime(LocalDate.now());
            int i = gardenWorkDao.insertSelective(gardenWork);
            return i > 0? true : false;
        }else {
            gardenWork.setUpdateTime(LocalDate.now());
            gardenWork.setUpdateName(UserRuntime.getUser().getUserName());
            int i = gardenWorkDao.updateByPrimaryKeySelective(gardenWork);
            return i > 0? true : false;
        }
    }

    @Override
    public boolean deltePlot(String id) {
        return gardenWorkDao.deleteByPrimaryKey(id) > 0? true : false;
    }
}
